package com.guodu.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.MaterialGhyctjDao;
import com.guodu.dao.UnitsDao;
import com.guodu.entity.MaterialGhyctj;
import com.guodu.entity.MaterialghyctjStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.entity.ViewMaterialGhyc;
import com.guodu.service.MaterialGhyctjService;
import com.guodu.vo.ViewMaterialGhycVo;
import com.guodu.vo.ViewVo;

@Service("materialGhyctjService")
public class MaterialGhyctjSerciceImpl implements MaterialGhyctjService {

	@Resource
	private MaterialGhyctjDao materialGhyctjDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Override
	public void add(MaterialGhyctj entity) {
		// TODO Auto-generated method stub
		materialGhyctjDao.add(entity);
	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		materialGhyctjDao.delete(id);
	}

	@Override
	public void update(MaterialGhyctj entity) {
		// TODO Auto-generated method stub
		materialGhyctjDao.update(entity);
	}

	@Override
	public Map<String, ViewMaterialGhycVo> findByVo(ViewVo vo,Units units) {
		// TODO Auto-generated method stub
		ViewVo viewVo = new  ViewVo();
		viewVo.setFunitsId(vo.getFunitsId());
		viewVo.setStatus(1L);
		viewVo.setTime(vo.getTime());
		viewVo.setUnitsId(vo.getUnitsId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Map<String,ViewMaterialGhycVo> map = new HashMap<String,ViewMaterialGhycVo>();
		map.put("all", new ViewMaterialGhycVo());
		List<Units> funits = unitsDao.findAllFunits();
		for(Units u : funits){
			map.put(u.getUnitsId()+"all", new ViewMaterialGhycVo());
		}
		try { 
		 if(sdf.parse(viewVo.getTime()).getTime()>sdf.parse("2013-9").getTime()){
				List<ViewMaterialGhyc> viewMaterialGhycs = materialGhyctjDao.findViewByVo(viewVo);
				ViewVo viewVo2 = new ViewVo();
				viewVo2.setFunitsId(viewVo.getFunitsId());
				viewVo2.setUnitsId(viewVo.getUnitsId());
				viewVo2.setTime((Integer.valueOf(viewVo.getTime().substring(0, 3))-1)+viewVo.getTime().substring(4,viewVo.getTime().length()));
				List<ViewMaterialGhyc> viewMaterialGhycs2 = materialGhyctjDao.findViewByVo(viewVo2);
				for(ViewMaterialGhyc viewMaterialGhyc :viewMaterialGhycs){
					if(viewMaterialGhyc.getFunitsId().intValue()==viewMaterialGhyc.getUnitsId().intValue()){
						this.sum2(map.get("all"), viewMaterialGhyc);
						this.sum2(map.get(viewMaterialGhyc.getFunitsId()+"all"),viewMaterialGhyc);
						map.put(viewMaterialGhyc.getUnitsId().toString(), new ViewMaterialGhycVo());
						this.sum2(map.get(viewMaterialGhyc.getUnitsId().toString()), viewMaterialGhyc);
					}
				}
				for(ViewMaterialGhyc viewMaterialGhyc :viewMaterialGhycs2){
					if(viewMaterialGhyc.getFunitsId().intValue()==viewMaterialGhyc.getUnitsId().intValue()){
					this.sum3(map.get("all"), viewMaterialGhyc);
					this.sum3(map.get(viewMaterialGhyc.getFunitsId()+"all"),viewMaterialGhyc);
					if(map.get(viewMaterialGhyc.getUnitsId().toString())!=null){
					this.sum3(map.get(viewMaterialGhyc.getUnitsId().toString()), viewMaterialGhyc);
					}
					}
				}
			}else{
				List<ViewMaterialGhyc> viewMaterialGhycs = materialGhyctjDao.findViewByVo(viewVo);
				ViewVo viewVo2 = new ViewVo();
				viewVo2.setFunitsId(viewVo.getFunitsId());
				viewVo2.setUnitsId(viewVo.getUnitsId());
				viewVo2.setTime((Integer.valueOf(viewVo.getTime().substring(0, 3))-1)+viewVo.getTime().substring(4,viewVo.getTime().length()));
				List<MaterialGhyctj> materialGhyctjs = materialGhyctjDao.findByVo(viewVo2,units);
				for(ViewMaterialGhyc viewMaterialGhyc :viewMaterialGhycs){
					this.sum2(map.get("all"), viewMaterialGhyc);
					if(viewMaterialGhyc.getFunitsId()!=null&&viewMaterialGhyc.getUnitsId()!=null){
						this.sum2(map.get(viewMaterialGhyc.getFunitsId()+"all"),viewMaterialGhyc);
						if(map.get(viewMaterialGhyc.getUnitsId().toString())==null){
							map.put(viewMaterialGhyc.getUnitsId().toString(), new ViewMaterialGhycVo(viewMaterialGhyc));
						}else{
						this.sum2(map.get(viewMaterialGhyc.getUnitsId().toString()), viewMaterialGhyc);
						}
					}
				}
				for(MaterialGhyctj materialGhyctj :materialGhyctjs){
					this.sum4(map.get("all"), materialGhyctj);
					this.sum4(map.get(materialGhyctj.getFunitsId()+"all"),materialGhyctj);
					if(map.get(materialGhyctj.getUnitsId().toString())!=null){
					this.sum4(map.get(materialGhyctj.getUnitsId().toString()), materialGhyctj);
					}
				}
			}
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public boolean isExist(Long unitsId, String month) {
		// TODO Auto-generated method stub
		return materialGhyctjDao.isExist(unitsId, month);
	}
	
	private void sum1(ViewMaterialGhycVo view1,MaterialGhyctj view2){
		view1.setCgMoney((view1.getCgMoney()!=null?view1.getCgMoney():new BigDecimal(0)).add(view2.getBycg()!=null?view2.getBycg():new BigDecimal(0)));
		view1.setAllCgMoney((view1.getAllCgMoney()!=null?view1.getAllCgMoney():new BigDecimal(0)).add(view2.getLjcg()!=null?view2.getLjcg():new BigDecimal(0)));
		view1.setBeforeCgMoney((view1.getBeforeCgMoney()!=null?view1.getBeforeCgMoney():new BigDecimal(0)).add(view2.getQntqcg()!=null?view2.getQntqcg():new BigDecimal(0)));
		view1.setXhMoney((view1.getXhMoney()!=null?view1.getXhMoney():new BigDecimal(0)).add(view2.getByxh()!=null?view2.getByxh():new BigDecimal(0)));
		view1.setAllXhMoney((view1.getAllXhMoney()!=null?view1.getAllXhMoney():new BigDecimal(0)).add(view2.getLjxh()!=null?view2.getLjxh():new BigDecimal(0)));
		view1.setBeforeXhMoney((view1.getBeforeXhMoney()!=null?view1.getBeforeXhMoney():new BigDecimal(0)).add(view2.getQntqxhj()!=null?view2.getQntqxhj():new BigDecimal(0)));
		view1.setBeginMoney((view1.getBeginMoney()!=null?view1.getBeginMoney():new BigDecimal(0)).add(view2.getSymkc()!=null?view2.getSymkc():new BigDecimal(0)));
		view1.setEndMoney((view1.getEndMoney()!=null?view1.getEndMoney():new BigDecimal(0)).add(view2.getBymkc()!=null?view2.getBymkc():new BigDecimal(0)));
		view1.setBeforeKcMoney((view1.getBeforeKcMoney()!=null?view1.getBeforeKcMoney():new BigDecimal(0)).add(view2.getQntqkc()!=null?view2.getQntqkc():new BigDecimal(0)));
		view1.setSubCgMoney((view1.getSubCgMoney()!=null?view1.getSubCgMoney():new BigDecimal(0)).add((view2.getBycg()!=null?view2.getBycg():new BigDecimal(0)).subtract(view2.getQntqcg()!=null?view2.getQntqcg():new BigDecimal(0))));
		view1.setSubKcMoney((view1.getSubKcMoney()!=null?view1.getSubKcMoney():new BigDecimal(0)).add((view2.getBymkc()!=null?view2.getBymkc():new BigDecimal(0)).subtract(view2.getQntqkc()!=null?view2.getQntqkc():new BigDecimal(0))));
		view1.setSubXhMoney((view1.getSubXhMoney()!=null?view1.getSubXhMoney():new BigDecimal(0)).add((view2.getByxh()!=null?view2.getByxh():new BigDecimal(0)).subtract(view2.getQntqxhj()!=null?view2.getQntqxhj():new BigDecimal(0))));
	}
	
	
	private void sum2(ViewMaterialGhycVo view1,ViewMaterialGhyc view2){
		view1.setCgMoney((view1.getCgMoney()!=null?view1.getCgMoney():new BigDecimal(0)).add(view2.getCgMoney()!=null?view2.getCgMoney():new BigDecimal(0)));
		view1.setAllCgMoney((view1.getAllCgMoney()!=null?view1.getAllCgMoney():new BigDecimal(0)).add(view2.getAllCgMoney()!=null?view2.getAllCgMoney():new BigDecimal(0)));
		view1.setXhMoney((view1.getXhMoney()!=null?view1.getXhMoney():new BigDecimal(0)).add(view2.getXhMoney()!=null?view2.getXhMoney():new BigDecimal(0)));
		view1.setAllXhMoney((view1.getAllXhMoney()!=null?view1.getAllXhMoney():new BigDecimal(0)).add(view2.getAllXhMoney()!=null?view2.getAllXhMoney():new BigDecimal(0)));
		view1.setBeginMoney((view1.getBeginMoney()!=null?view1.getBeginMoney():new BigDecimal(0)).add(view2.getBeginMoney()!=null?view2.getBeginMoney():new BigDecimal(0)));
		view1.setEndMoney((view1.getEndMoney()!=null?view1.getEndMoney():new BigDecimal(0)).add(view2.getEndMoney()!=null?view2.getEndMoney():new BigDecimal(0)));

	}
	
	private void sum3(ViewMaterialGhycVo view1,ViewMaterialGhyc view2){
		view1.setBeforeCgMoney((view1.getBeforeCgMoney()!=null?view1.getBeforeCgMoney():new BigDecimal(0)).add(view2.getCgMoney()!=null?view2.getCgMoney():new BigDecimal(0)));
		view1.setBeforeXhMoney((view1.getBeforeXhMoney()!=null?view1.getBeforeXhMoney():new BigDecimal(0)).add(view2.getXhMoney()!=null?view2.getXhMoney():new BigDecimal(0)));
		view1.setBeforeKcMoney((view1.getBeforeKcMoney()!=null?view1.getBeforeKcMoney():new BigDecimal(0)).add(view2.getEndMoney()!=null?view2.getEndMoney():new BigDecimal(0)));
		if(view1.getCgMoney()!=null&&view1.getBeforeCgMoney()!=null){
			view1.setSubCgMoney((view1.getSubCgMoney()!=null?view1.getSubCgMoney():new BigDecimal(0)).add((view1.getCgMoney()!=null?view1.getCgMoney():new BigDecimal(0)).subtract(view1.getBeforeCgMoney()!=null?view1.getBeforeCgMoney():new BigDecimal(0))));
		}
		if(view1.getEndMoney()!=null&&view1.getBeforeKcMoney()!=null){
			view1.setSubKcMoney((view1.getSubKcMoney()!=null?view1.getSubKcMoney():new BigDecimal(0)).add((view1.getEndMoney()!=null?view1.getEndMoney():new BigDecimal(0)).subtract(view1.getBeforeKcMoney()!=null?view1.getBeforeKcMoney():new BigDecimal(0))));
		}
		if(view1.getXhMoney()!=null&&view1.getBeforeXhMoney()!=null){
			view1.setSubXhMoney((view1.getSubXhMoney()!=null?view1.getSubXhMoney():new BigDecimal(0)).add((view1.getXhMoney()!=null?view1.getXhMoney():new BigDecimal(0)).subtract(view1.getBeforeXhMoney()!=null?view1.getBeforeXhMoney():new BigDecimal(0))));
		}
	}
	
	private void sum4(ViewMaterialGhycVo view1,MaterialGhyctj view2){
		view1.setBeforeCgMoney((view1.getBeforeCgMoney()!=null?view1.getBeforeCgMoney():new BigDecimal(0)).add(view2.getLjcg()!=null?view2.getLjcg():new BigDecimal(0)));
		view1.setBeforeXhMoney((view1.getBeforeXhMoney()!=null?view1.getBeforeXhMoney():new BigDecimal(0)).add(view2.getLjxh()!=null?view2.getLjxh():new BigDecimal(0)));
		view1.setBeforeKcMoney((view1.getBeforeKcMoney()!=null?view1.getBeforeKcMoney():new BigDecimal(0)).add(view2.getBymkc()!=null?view2.getBymkc():new BigDecimal(0)));
		if(view1.getCgMoney()!=null&&view1.getBeforeCgMoney()!=null){
			view1.setSubCgMoney((view1.getSubCgMoney()!=null?view1.getSubCgMoney():new BigDecimal(0)).add((view1.getCgMoney()!=null?view1.getCgMoney():new BigDecimal(0)).subtract(view1.getBeforeCgMoney()!=null?view1.getBeforeCgMoney():new BigDecimal(0))));
		}
		if(view1.getEndMoney()!=null&&view1.getBeforeKcMoney()!=null){
			view1.setSubKcMoney((view1.getSubKcMoney()!=null?view1.getSubKcMoney():new BigDecimal(0)).add((view1.getEndMoney()!=null?view1.getEndMoney():new BigDecimal(0)).subtract(view1.getBeforeKcMoney()!=null?view1.getBeforeKcMoney():new BigDecimal(0))));
		}
		if(view1.getXhMoney()!=null&&view1.getBeforeXhMoney()!=null){
			view1.setSubXhMoney((view1.getSubXhMoney()!=null?view1.getSubXhMoney():new BigDecimal(0)).add((view1.getXhMoney()!=null?view1.getXhMoney():new BigDecimal(0)).subtract(view1.getBeforeXhMoney()!=null?view1.getBeforeXhMoney():new BigDecimal(0))));
		}
	}

	@Override
	public MaterialGhyctj findById(String id) {
		// TODO Auto-generated method stub
		return materialGhyctjDao.findById(id);
	}

	@Override
	public List<MaterialGhyctj> findAllByVo(ViewVo viewVo, Units units) {
		// TODO Auto-generated method stub
		return materialGhyctjDao.findByVo(viewVo, units);
	}

	@Override
	public void examine(String ids, String reason, String beforeStatus,
			Long afterStatus, UserInfo userInfo) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<MaterialghyctjStatus> entities = new ArrayList<MaterialghyctjStatus>();
		for(int i=0;i<idses.length;i++){
			MaterialghyctjStatus entity = new MaterialghyctjStatus();
			entity.setAfterStatus(afterStatus);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(userInfo.getUserId());
			entity.setMaterialId(idses[i]);
			entity.setReason(reason);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		materialGhyctjDao.changeStatus(entities);
		materialGhyctjDao.examine(ids,afterStatus);
	}

	@Override
	public List<MaterialghyctjStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return materialGhyctjDao.findStatus(id);
	}
	

}
